package main

import (
	"fmt"
	"natsmicro/conf"
	"os"
	"os/signal"
	"syscall"

	"github.com/gogf/gf/frame/g"
	"github.com/nats-io/nats.go"
)

func main() {

	nc, _ := nats.Connect(conf.NatsUrl)
	js, err := nc.JetStream()

	if err != nil {
		g.Log().Fatal(err)
	}

	// js.Subscribe(conf.SubjectNameWild, func(msg *nats.Msg) {
	// 	msg.Ack()
	// 	fmt.Printf("Subscribe: %s\n", string(msg.Data))
	// 	time.Sleep(1 * time.Second)

	// }, nats.DeliverAll(), nats.Durable("ssss"))
	for ii := 0; ii < 10; ii++ {
		jj := ii
		js.QueueSubscribe(conf.SubjectNameWild, "aaab", func(msg *nats.Msg) {
			msg.Ack()
			fmt.Println("QueueSubscribe: ", string(msg.Data), msg.Subject, jj)
			// time.Sleep(1 * time.Millisecond)
		}, nats.DeliverAll())
	}

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGUSR1, syscall.SIGUSR2)
	fmt.Println("启动")
	s := <-c
	fmt.Println("退出信号", s)

	nc.Close()
}
